package com.miplus.ccrm.sms;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.miplus.ccrm.common.EnumRCode;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.common.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @Author： Yaohui
 * @Date： 2024/12/2 15:11
 * @Describe：
 */
@Slf4j
@Service
public class OnLineApiService {


    @Value("${ccrm.sms.url}")
    private String smsUrl;

    private static String srcCnsmrSysId="NDCS";
    private static String cnsmrSysId="NDCS";
    private static String cnsmrMrchId="NDCS";

//    Mp=融资中台

    /**
     * 融资中台借据明细
     * @param mrchId
     * @param identNo
     * @param identTp
     * @param dblNo
     * @param statusL
     * @param pdId 产品id
     * @return
     */
    public LineAPIR getCLMPLoanLnvoiceInfo(String mrchId,String identNo,String identTp,
                                           String dblNo,
                                           Integer statusL,String pdId){

        String adrIP= JPubUtils.getLocalIpAddr();

        JSONObject objJson=new JSONObject();
        Date currentDate =new Date();
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmmssSSS");
        SimpleDateFormat TxnDtF = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat rqsTmF = new SimpleDateFormat("yyyy-MM-dd HH:MM:SS");
        Random random=new Random();
        String tDt=sdf.format(currentDate)+(100+random.nextInt(900));
        String TxnDt= TxnDtF.format(currentDate);
        String rqsTm = rqsTmF.format(currentDate);

        JSONObject headJson=new JSONObject();
        headJson.put("SvcCd","30230001");
        headJson.put("SvcScn","57");
        headJson.put("CnlTp","07");
        headJson.put("CnsmrSysId",cnsmrSysId);
        headJson.put("CnsmrSeqNo",tDt);
        headJson.put("BsnSeqNo",tDt);
        headJson.put("SrcCnsmrSysId",srcCnsmrSysId);
        headJson.put("SrcCnsmrSeqNo",tDt);
        headJson.put("TxnDt",TxnDt);//时间戳
        headJson.put("TxnTm","195242800");
        headJson.put("CnsmrSvrId","");
        headJson.put("RntrId","0");
        headJson.put("RqsTm",rqsTm);
        headJson.put("IPAdr",adrIP);
        headJson.put("MrchId",cnsmrMrchId);


//        headJson.put("UsrLng","CHINESE");
//        headJson.put("InstId","50801");
//        headJson.put("TlrNo","V0016");
//        headJson.put("FileFlg","0");

        objJson.put("Head",headJson);
        JSONObject bodyJson=new JSONObject();
        bodyJson.put("IdentNo",identNo);
        bodyJson.put("IdentTp",identTp);
        if(JPubUtils.IsNotEmpty(dblNo)){
            bodyJson.put("DblNo",dblNo);//借据号
        }
        if(JPubUtils.IsNotEmpty(pdId)){
            bodyJson.put("PdCd",pdId);//产品号
        }
        objJson.put("Body",bodyJson);

        String requestBody=objJson.toJSONString();

        log.info("SendSms:{}",requestBody);


        try {
            String sendUrl=smsUrl+"/NDCS/CLMP036";
            // 发送 POST 请求
            HttpResponse<JsonNode> jsonResponse = Unirest.post(sendUrl)
                    .header("Content-Type", "application/json;charset=UTF-8")
                    .body(requestBody)
                    .asJson();

            int status = jsonResponse.getStatus();
            if (status == 200) {
                log.info("SendSms.Response:{}", jsonResponse.getBody());

                String retCd = JSONObject.parseObject(jsonResponse.getBody().getObject()
                        .get("Head").toString()).get("RetCd").toString();

                log.info("retCd::{}",retCd);

                if(!retCd.equals("000000")){
                    throw new ServiceException(EnumRCode.NoData.value, EnumRCode.NoData.name);
                }
                JSONObject jsonObject = JSONObject.parseObject(jsonResponse.getBody().getObject().get("Body").toString());

                if(statusL==2){
                    //保留逾期数据
                    List<JSONObject> jsonObjectList=new ArrayList<>();
                    JSONArray dblInfArry = jsonObject.getJSONArray("DblInfArry");
                    for (int i = 0; i < dblInfArry.size(); i++) {
                        JSONObject object = dblInfArry.getJSONObject(i);
                        if(!"2".equals(object.get("St").toString())){
                            jsonObjectList.add(object);
                        }
                    }
                    for (int i = 0; i < jsonObjectList.size(); i++) {
                        dblInfArry.remove(jsonObjectList.get(i));//去除逾期之外的数据
                    }

                }

                return LineAPIR.Success("ok",jsonObject);
            } else {
                log.warn("SendSms.Fail:Status={},Cause={}", status, jsonResponse.getStatusText());
                return LineAPIR.Error(status,jsonResponse.getStatusText());
            }
        }
        catch (Exception e){
            log.warn("SendSms.Err:{}", e.getMessage());

            return LineAPIR.Error(-1,e.getMessage());
        }
    }


    /**
     * 融资中台还款计划
     * @param mrchId  商户号
     * @param dblNo 借据号
     * @param repymtPlnSt 状态 4=逾期
     * @return
     */
    public LineAPIR getCLMPRepayPlan(String mrchId,String dblNo,String repymtPlnSt){

        String adrIP= JPubUtils.getLocalIpAddr();

        JSONObject objJson=new JSONObject();
        Date currentDate =new Date();
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmmssSSS");
        SimpleDateFormat TxnDtF = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat rqsTmF = new SimpleDateFormat("yyyy-MM-dd HH:MM:SS");
        Random random=new Random();
        String tDt=sdf.format(currentDate)+(100+random.nextInt(900));
        String TxnDt= TxnDtF.format(currentDate);
        String rqsTm = rqsTmF.format(currentDate);

        JSONObject headJson=new JSONObject();
        headJson.put("SvcCd","30230005");
        headJson.put("SvcScn","09");
        headJson.put("CnlTp","07");
        headJson.put("CnsmrSysId",cnsmrSysId);
        headJson.put("CnsmrSeqNo",tDt);
        headJson.put("BsnSeqNo",tDt);
        headJson.put("SrcCnsmrSysId",srcCnsmrSysId);
        headJson.put("SrcCnsmrSeqNo",tDt);
        headJson.put("TxnDt",TxnDt);//时间戳
        headJson.put("TxnTm","195242800");
        headJson.put("CnsmrSvrId","");
        headJson.put("RntrId","0");
        headJson.put("RqsTm",rqsTm);
        headJson.put("IPAdr",adrIP);
        headJson.put("MrchId",cnsmrMrchId);

        objJson.put("Head",headJson);
        JSONObject bodyJson=new JSONObject();
        bodyJson.put("DblNo",dblNo);
        if(JPubUtils.IsNotEmpty(repymtPlnSt)){
            bodyJson.put("RepymtPlnSt",repymtPlnSt);
        }
        objJson.put("Body",bodyJson);

        String requestBody=objJson.toJSONString();

        log.info("SendSms:{}",requestBody);


        try {
            String sendUrl=smsUrl+"/NDCS/CLMP050";
            // 发送 POST 请求
            HttpResponse<JsonNode> jsonResponse = Unirest.post(sendUrl)
                    .header("Content-Type", "application/json;charset=UTF-8")
                    .body(requestBody)
                    .asJson();

            int status = jsonResponse.getStatus();
            if (status == 200) {
                log.info("SendSms.Response:{}", jsonResponse.getBody());

                String retCd = JSONObject.parseObject(jsonResponse.getBody().getObject()
                        .get("Head").toString()).get("RetCd").toString();

                log.info("retCd::{}",retCd);

                if(!retCd.equals("000000")){
                    throw new ServiceException(EnumRCode.NoData.value, EnumRCode.NoData.name);
                }

                JSONObject jsonObject = JSONObject.parseObject(jsonResponse.getBody().getObject().get("Body").toString());

                return LineAPIR.Success("ok",jsonObject);
            } else {
                log.warn("SendSms.Fail:Status={},Cause={}", status, jsonResponse.getStatusText());
                return LineAPIR.Error(status,jsonResponse.getStatusText());
            }
        }
        catch (Exception e){
            log.warn("SendSms.Err:{}", e.getMessage());

            return LineAPIR.Error(-1,e.getMessage());
        }
    }




    //还款记录|流水
    public LineAPIR getCLMPRepaySerial(Integer pgNo,Integer pgLineNum,String mrchId,String identNo,String identTp,String dblNo,
                                       String pdId){

        String adrIP= JPubUtils.getLocalIpAddr();

        JSONObject objJson=new JSONObject();
        Date currentDate =new Date();
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmmssSSS");
        SimpleDateFormat TxnDtF = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat rqsTmF = new SimpleDateFormat("yyyy-MM-dd HH:MM:SS");
        Random random=new Random();
        String tDt=sdf.format(currentDate)+(100+random.nextInt(900));
        String TxnDt= TxnDtF.format(currentDate);
        String rqsTm = rqsTmF.format(currentDate);

        JSONObject headJson=new JSONObject();
        headJson.put("SvcCd","30230001");
        headJson.put("SvcScn","59");
        headJson.put("CnlTp","07");
        headJson.put("CnsmrSysId",cnsmrSysId);
        headJson.put("CnsmrSeqNo",tDt);
        headJson.put("BsnSeqNo",tDt);
        headJson.put("SrcCnsmrSysId",srcCnsmrSysId);
        headJson.put("SrcCnsmrSeqNo",tDt);
        headJson.put("TxnDt",TxnDt);//时间戳
        headJson.put("TxnTm","195242800");
        headJson.put("CnsmrSvrId","");
        headJson.put("RntrId","0");
        headJson.put("RqsTm",rqsTm);
        headJson.put("IPAdr",adrIP);
        headJson.put("MrchId",cnsmrMrchId);

        objJson.put("Head",headJson);
        JSONObject bodyJson=new JSONObject();
        bodyJson.put("IdentTp",identTp);
        bodyJson.put("IdentNo",identNo);
        bodyJson.put("DblNo",dblNo);
        bodyJson.put("PdId",pdId);
        bodyJson.put("PgNo",pgNo);
        bodyJson.put("PgLineNum",pgLineNum);
        objJson.put("Body",bodyJson);

        String requestBody=objJson.toJSONString();

        log.info("SendSms:{}",requestBody);


        try {
            String sendUrl=smsUrl+"/NDCS/CLMP054";
            // 发送 POST 请求
            HttpResponse<JsonNode> jsonResponse = Unirest.post(sendUrl)
                    .header("Content-Type", "application/json;charset=UTF-8")
                    .body(requestBody)
                    .asJson();

            int status = jsonResponse.getStatus();
            if (status == 200) {
                log.info("SendSms.Response:{}", jsonResponse.getBody());

                String retCd = JSONObject.parseObject(jsonResponse.getBody().getObject()
                        .get("Head").toString()).get("RetCd").toString();

                log.info("retCd::{}",retCd);

                if(!retCd.equals("000000")){
                    throw new ServiceException(EnumRCode.NoData.value, EnumRCode.NoData.name);
                }

                JSONObject jsonObject = JSONObject.parseObject(jsonResponse.getBody().getObject().get("Body").toString());

                return LineAPIR.Success("ok",jsonObject);
            } else {
                log.warn("SendSms.Fail:Status={},Cause={}", status, jsonResponse.getStatusText());
                return LineAPIR.Error(status,jsonResponse.getStatusText());
            }
        }
        catch (Exception e){
            log.warn("SendSms.Err:{}", e.getMessage());

            return LineAPIR.Error(-1,e.getMessage());
        }
    }


    /**
     * 融资中台还款申请
     * @param mrchId 商户号
     * @param identNo  身份证号
     * @param dblNo  借据号
     * @param repymtAmt 还款金额
     * @param repymtMtd 1：一键扣款 2：支付宝还款
     * @param thirdRepymtSn 还款流水号 NDCS开头 默认拼接NDCS
     * @return
     * MsgUrl 短信链接URL 【支付宝付款有值】
     * PaySqlNo 支付流水号
     */
    public LineAPIR CLMPRepaymentApply(String mrchId,String identNo,String dblNo,BigDecimal repymtAmt,
                                       String repymtMtd,String thirdRepymtSn){

        String adrIP= JPubUtils.getLocalIpAddr();

        JSONObject objJson=new JSONObject();
        Date currentDate =new Date();
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmmssSSS");
        SimpleDateFormat TxnDtF = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat rqsTmF = new SimpleDateFormat("yyyy-MM-dd HH:MM:SS");
        Random random=new Random();
        String tDt=sdf.format(currentDate)+(100+random.nextInt(900));
        String TxnDt= TxnDtF.format(currentDate);
        String rqsTm = rqsTmF.format(currentDate);

        JSONObject headJson=new JSONObject();
        headJson.put("SvcCd","30210001");
        headJson.put("SvcScn","07");
        headJson.put("CnlTp","07");
        headJson.put("CnsmrSysId",cnsmrSysId);
        headJson.put("CnsmrSeqNo",tDt);
        headJson.put("BsnSeqNo",tDt);
        headJson.put("SrcCnsmrSysId",srcCnsmrSysId);
        headJson.put("SrcCnsmrSeqNo",tDt);
        headJson.put("TxnDt",TxnDt);//时间戳
        headJson.put("TxnTm","195242800");
        headJson.put("CnsmrSvrId","");
        headJson.put("RntrId","0");
        headJson.put("RqsTm",rqsTm);
        headJson.put("IPAdr",adrIP);
        headJson.put("MrchId",cnsmrMrchId);

        objJson.put("Head",headJson);
        JSONObject bodyJson=new JSONObject();
        bodyJson.put("IdentNo",identNo);
        bodyJson.put("DblNo",dblNo);
        bodyJson.put("RepymtAmt",repymtAmt);
        bodyJson.put("Ccy","CNY");
        bodyJson.put("RepymtMtd",repymtMtd);
        bodyJson.put("ThirdRepymtSn","NDCS"+thirdRepymtSn);
        bodyJson.put("RepymtTp","2");
        objJson.put("Body",bodyJson);

        String requestBody=objJson.toJSONString();

        log.info("SendSms:{}",requestBody);


        try {
            String sendUrl=smsUrl+"/NDCS/CLMP065";
            // 发送 POST 请求
            HttpResponse<JsonNode> jsonResponse = Unirest.post(sendUrl)
                    .header("Content-Type", "application/json;charset=UTF-8")
                    .body(requestBody)
                    .asJson();

            int status = jsonResponse.getStatus();
            if (status == 200) {
                log.info("SendSms.Response:{}", jsonResponse.getBody());

                String retCd = JSONObject.parseObject(jsonResponse.getBody().getObject()
                        .get("Head").toString()).get("RetCd").toString();

                log.info("retCd::{}",retCd);

                if(!retCd.equals("000000")){

                    String retMsg = JSONObject.parseObject(jsonResponse.getBody().getObject()
                            .get("Head").toString()).get("RetMsg").toString();

                    throw new ServiceException(EnumRCode.FeignErr.value, retMsg);
                }

                return LineAPIR.Success("ok",JSONObject.parseObject(jsonResponse.getBody().getObject().get("Body").toString()));
            } else {
                log.warn("SendSms.Fail:Status={},Cause={}", status, jsonResponse.getStatusText());
                return LineAPIR.Error(status,jsonResponse.getStatusText());
            }
        }
        catch (Exception e){
            log.warn("SendSms.Err:{}", e.getMessage());

            return LineAPIR.Error(-1,e.getMessage());
        }
    }


    //扣款结果
    public LineAPIR getCLMPRepaymentResult(String mrchId,String thirdRepymtSn){


        String adrIP= JPubUtils.getLocalIpAddr();

        JSONObject objJson=new JSONObject();
        Date currentDate =new Date();
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmmssSSS");
        SimpleDateFormat TxnDtF = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat rqsTmF = new SimpleDateFormat("yyyy-MM-dd HH:MM:SS");
        Random random=new Random();
        String tDt=sdf.format(currentDate)+(100+random.nextInt(900));
        String TxnDt= TxnDtF.format(currentDate);
        String rqsTm = rqsTmF.format(currentDate);

        JSONObject headJson=new JSONObject();
        headJson.put("SvcCd","30230005");
        headJson.put("SvcScn","10");
        headJson.put("CnlTp","07");
        headJson.put("CnsmrSysId",cnsmrSysId);
        headJson.put("CnsmrSeqNo",tDt);
        headJson.put("BsnSeqNo",tDt);
        headJson.put("SrcCnsmrSysId",srcCnsmrSysId);
        headJson.put("SrcCnsmrSeqNo",tDt);
        headJson.put("TxnDt",TxnDt);//时间戳
        headJson.put("TxnTm","195242800");
        headJson.put("CnsmrSvrId","");
        headJson.put("RntrId","0");
        headJson.put("RqsTm",rqsTm);
        headJson.put("IPAdr",adrIP);
        headJson.put("MrchId",cnsmrMrchId);

        objJson.put("Head",headJson);
        JSONObject bodyJson=new JSONObject();
        bodyJson.put("ThirdRepymtSn","NDCS"+thirdRepymtSn);
        objJson.put("Body",bodyJson);

        String requestBody=objJson.toJSONString();

        log.info("SendSms:{}",requestBody);


        try {
            String sendUrl=smsUrl+"/NDCS/CLMP070";
            // 发送 POST 请求
            HttpResponse<JsonNode> jsonResponse = Unirest.post(sendUrl)
                    .header("Content-Type", "application/json;charset=UTF-8")
                    .body(requestBody)
                    .asJson();

            int status = jsonResponse.getStatus();
            if (status == 200) {
                log.info("SendSms.Response:{}", jsonResponse.getBody());

                String retCd = JSONObject.parseObject(jsonResponse.getBody().getObject()
                        .get("Head").toString()).get("RetCd").toString();

                log.info("retCd::{}",retCd);

                if(!retCd.equals("000000")){
                    throw new ServiceException(EnumRCode.NoData.value, EnumRCode.NoData.name);
                }

                return LineAPIR.Success("ok",JSONObject.parseObject(jsonResponse.getBody().getObject().get("Body").toString()));
            } else {
                log.warn("SendSms.Fail:Status={},Cause={}", status, jsonResponse.getStatusText());
                return LineAPIR.Error(status,jsonResponse.getStatusText());
            }
        }
        catch (Exception e){
            log.warn("SendSms.Err:{}", e.getMessage());

            return LineAPIR.Error(-1,e.getMessage());
        }
    }


    /**
     * 额度刷新
     * @param mrchId
     * @param identNo
     * @param identTp
     * @param pdId
     * @return
     */
    public LineAPIR refreshCLMPCreditLimit(String mrchId,String identNo,String identTp,String pdId){

        String adrIP= JPubUtils.getLocalIpAddr();

        JSONObject objJson=new JSONObject();
        Date currentDate =new Date();
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmmssSSS");
        SimpleDateFormat TxnDtF = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat rqsTmF = new SimpleDateFormat("yyyy-MM-dd HH:MM:SS");
        Random random=new Random();
        String tDt=sdf.format(currentDate)+(100+random.nextInt(900));
        String TxnDt= TxnDtF.format(currentDate);
        String rqsTm = rqsTmF.format(currentDate);

        JSONObject headJson=new JSONObject();
        headJson.put("SvcCd","30230001");
        headJson.put("SvcScn","58");
        headJson.put("CnlTp","07");
        headJson.put("CnsmrSysId",cnsmrSysId);
        headJson.put("CnsmrSeqNo",tDt);
        headJson.put("BsnSeqNo",tDt);
        headJson.put("SrcCnsmrSysId",srcCnsmrSysId);
        headJson.put("SrcCnsmrSeqNo",tDt);
        headJson.put("TxnDt",TxnDt);//时间戳
        headJson.put("TxnTm","195242800");
        headJson.put("CnsmrSvrId","");
        headJson.put("RntrId","0");
        headJson.put("RqsTm",rqsTm);
        headJson.put("IPAdr",adrIP);
        headJson.put("MrchId",cnsmrMrchId);

        objJson.put("Head",headJson);
        JSONObject bodyJson=new JSONObject();
        bodyJson.put("IdentTp",identTp);
        bodyJson.put("IdentNo",identNo);
        bodyJson.put("PdId",pdId);
        objJson.put("Body",bodyJson);

        String requestBody=objJson.toJSONString();

        log.info("SendSms:{}",requestBody);


        try {
            String sendUrl=smsUrl+"/NDCS/CLMP039";
            // 发送 POST 请求
            HttpResponse<JsonNode> jsonResponse = Unirest.post(sendUrl)
                    .header("Content-Type", "application/json;charset=UTF-8")
                    .body(requestBody)
                    .asJson();

            int status = jsonResponse.getStatus();
            if (status == 200) {
                log.info("SendSms.Response:{}", jsonResponse.getBody());

                String retCd = JSONObject.parseObject(jsonResponse.getBody().getObject()
                        .get("Head").toString()).get("RetCd").toString();

                log.info("retCd::{}",retCd);

                if(!retCd.equals("000000")){
                    throw new ServiceException(EnumRCode.NoData.value, EnumRCode.NoData.name);
                }

                JSONObject jsonObject = JSONObject.parseObject(jsonResponse.getBody().getObject().get("Body").toString());

                JSONObject jA=new JSONObject();
                JSONArray jsonArray=new JSONArray();
                jsonArray.add(jsonObject);
                jA.put("creditArray",jsonArray);
                return LineAPIR.Success("ok",jA);
            } else {
                log.warn("SendSms.Fail:Status={},Cause={}", status, jsonResponse.getStatusText());
                return LineAPIR.Error(status,jsonResponse.getStatusText());
            }
        }
        catch (Exception e){
            log.warn("SendSms.Err:{}", e.getMessage());

            return LineAPIR.Error(-1,e.getMessage());
        }

    }

    /**
     * 智慧线上额度刷新
     * @param pdNo
     * @param identCrdNo
     * @return
     */
    public LineAPIR refreshSOFPCreditLimit(String pdNo,String identCrdNo){
//        String adrIP= JPubUtils.getLocalIpAddr();

        JSONObject objJson=new JSONObject();
        Date currentDate =new Date();
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmmssSSS");
        SimpleDateFormat TxnDtF = new SimpleDateFormat("yyyyMMdd");
//        SimpleDateFormat RqsTmF = new SimpleDateFormat("yyyy-MM-dd HH:MM:SS");
        Random random=new Random();
        String tDt=sdf.format(currentDate)+(100+random.nextInt(900));
        String TxnDt= TxnDtF.format(currentDate);
//        String RqsTm = RqsTmF.format(currentDate);

        JSONObject headJson=new JSONObject();
        headJson.put("SvcCd","30230001");
        headJson.put("SvcScn","29");
        headJson.put("UsrLng","CHINESE");
        headJson.put("MdulId","RB");
        headJson.put("CnlTp","07");
        headJson.put("CnsmrSysId","101500");
        headJson.put("CnsmrSeqNo",tDt);
        headJson.put("BsnSeqNo",tDt);
        headJson.put("SrcCnsmrSysId","100900");
        headJson.put("SrcCnsmrSeqNo",tDt);
        headJson.put("TxnDt",TxnDt);//时间戳
        headJson.put("TxnTm","195242800");
        headJson.put("CnsmrSvrId","1234567");
        headJson.put("FileFlg","0");
//        headJson.put("InstId","50801");
//        headJson.put("TlrNo","V0016");

        objJson.put("Head",headJson);
        JSONObject bodyJson=new JSONObject();
        bodyJson.put("PdNo",pdNo);
        bodyJson.put("IdentCrdNo",identCrdNo);
        objJson.put("Body",bodyJson);
        String requestBody=objJson.toJSONString();

        log.info("SendSms:{}",requestBody);

        try {
            String sendUrl=smsUrl+"/NDCS";
            // 发送 POST 请求
            HttpResponse<JsonNode> jsonResponse = Unirest.post(sendUrl)
                    .header("Content-Type", "application/json;charset=UTF-8")
                    .body(requestBody)
                    .asJson();

            int status = jsonResponse.getStatus();
            if (status == 200) {
                log.info("SendSms.Response:{}", jsonResponse.getBody());

                String subRetCd = JSONObject.parseObject(jsonResponse.getBody().getObject()
                        .get("Head").toString()).get("SubRetCd").toString();

                log.info("subRetCd::{}",subRetCd);

                if(subRetCd.equals("F")){
                    throw new ServiceException(EnumRCode.NoData.value, EnumRCode.NoData.name);
                }

                return LineAPIR.Success("ok",JSONObject.parseObject(jsonResponse.getBody().getObject().get("Body").toString()));
            } else {
                log.warn("SendSms.Fail:Status={},Cause={}", status, jsonResponse.getStatusText());
                return LineAPIR.Error(status,jsonResponse.getStatusText());
            }
        }
        catch (Exception e){
            log.warn("SendSms.Err:{}", e.getMessage());
            return LineAPIR.Error(-1,e.getMessage());
        }
    }


    /**
     * 智慧线上借据明细
     * @param ctrSeqNo 合同流水号
     * @param identCrdNo 身份证号
     * @param statusL 状态
     * @return
     */
    public LineAPIR getSOFPLoanLnvoiceInfo(String ctrSeqNo,
                                           String identCrdNo,
                                           Integer statusL){

//        String adrIP= JPubUtils.getLocalIpAddr();

        JSONObject objJson=new JSONObject();
        Date currentDate =new Date();
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmmssSSS");
        SimpleDateFormat TxnDtF = new SimpleDateFormat("yyyyMMdd");
//        SimpleDateFormat RqsTmF = new SimpleDateFormat("yyyy-MM-dd HH:MM:SS");
        Random random=new Random();
        String tDt=sdf.format(currentDate)+(100+random.nextInt(900));
        String TxnDt= TxnDtF.format(currentDate);
//        String RqsTm = RqsTmF.format(currentDate);

        JSONObject headJson=new JSONObject();
        headJson.put("SvcCd","30230001");
        headJson.put("SvcScn","33");
        headJson.put("UsrLng","CHINESE");
        headJson.put("MdulId","RB");
        headJson.put("CnlTp","07");
        headJson.put("CnsmrSysId","101500");
        headJson.put("CnsmrSeqNo",tDt);
        headJson.put("BsnSeqNo",tDt);
        headJson.put("SrcCnsmrSysId","100900");
        headJson.put("SrcCnsmrSeqNo",tDt);
        headJson.put("TxnDt",TxnDt);//时间戳
        headJson.put("TxnTm","195242800");
        headJson.put("CnsmrSvrId","1234567");
        headJson.put("FileFlg","0");
//        headJson.put("InstId","50801");
//        headJson.put("TlrNo","V0016");

        objJson.put("Head",headJson);
        JSONObject bodyJson=new JSONObject();
        bodyJson.put("CtrSeqNo",ctrSeqNo);
        bodyJson.put("IdentCrdNo",identCrdNo);
        objJson.put("Body",bodyJson);
        String requestBody=objJson.toJSONString();

        log.info("SendSms:{}",requestBody);

        try {
            String sendUrl=smsUrl+"/NDCS";
            // 发送 POST 请求
            HttpResponse<JsonNode> jsonResponse = Unirest.post(sendUrl)
                    .header("Content-Type", "application/json;charset=UTF-8")
                    .body(requestBody)
                    .asJson();

            int status = jsonResponse.getStatus();
            if (status == 200) {
                log.info("SendSms.Response:{}", jsonResponse.getBody());

                String subRetCd = JSONObject.parseObject(jsonResponse.getBody().getObject()
                        .get("Head").toString()).get("SubRetCd").toString();

                log.info("subRetCd::{}",subRetCd);

                if(subRetCd.equals("F")){
                    throw new ServiceException(EnumRCode.NoData.value, EnumRCode.NoData.name);
                }

                //移除冗余数据
                JSONObject jsonObject = JSONObject.parseObject(jsonResponse.getBody().getObject().get("Body").toString());
                jsonObject.remove("CtrseqNo");
                jsonObject.remove("IdentCrdNo");


                if(statusL==2){
                    //保留逾期借据
                    List<JSONObject>jsonObjectList=new ArrayList<>();
                    JSONArray dblLstArry = jsonObject.getJSONArray("DblLstArry");
                    for (int i = 0; i < dblLstArry.size(); i++) {
                        JSONObject object = dblLstArry.getJSONObject(i);
                        if(!"1".equals(object.get("LoanSt").toString())){//是否是逾期 1
                            jsonObjectList.add(object);
                        };
                    }

                    for (int i = 0; i < jsonObjectList.size(); i++) {
                        dblLstArry.remove(jsonObjectList.get(i));
                    }
                }

                return LineAPIR.Success("ok",jsonObject);
            } else {
                log.warn("SendSms.Fail:Status={},Cause={}", status, jsonResponse.getStatusText());
                return LineAPIR.Error(status,jsonResponse.getStatusText());
            }
        }
        catch (Exception e){
            log.warn("SendSms.Err:{}", e.getMessage());
            return LineAPIR.Error(-1,e.getMessage());
        }
    }


    /**
     * 智慧线上还款计划
     * @param loanDblNo 借据号
     * @param identCrdNo 身份证号
     * @return
     */
    public LineAPIR getSOFPRepayPlan(String loanDblNo,String identCrdNo){

//        String adrIP= JPubUtils.getLocalIpAddr();

        JSONObject objJson=new JSONObject();
        Date currentDate =new Date();
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmmssSSS");
        SimpleDateFormat TxnDtF = new SimpleDateFormat("yyyyMMdd");
//        SimpleDateFormat RqsTmF = new SimpleDateFormat("yyyy-MM-dd HH:MM:SS");
        Random random=new Random();
        String tDt=sdf.format(currentDate)+(100+random.nextInt(900));
        String TxnDt= TxnDtF.format(currentDate);
//        String RqsTm = RqsTmF.format(currentDate);

        JSONObject headJson=new JSONObject();
        headJson.put("SvcCd","30230005");
        headJson.put("SvcScn","04");
        headJson.put("UsrLng","CHINESE");
        headJson.put("MdulId","RB");
        headJson.put("CnlTp","07");
        headJson.put("CnsmrSysId","101500");
        headJson.put("CnsmrSeqNo",tDt);
        headJson.put("BsnSeqNo",tDt);
        headJson.put("SrcCnsmrSysId","100900");
        headJson.put("SrcCnsmrSeqNo",tDt);
        headJson.put("TxnDt",TxnDt);//时间戳
        headJson.put("TxnTm","195242800");
        headJson.put("CnsmrSvrId","1234567");
        headJson.put("FileFlg","0");
//        headJson.put("InstId","50801");
//        headJson.put("TlrNo","V0016");

        objJson.put("Head",headJson);
        JSONObject bodyJson=new JSONObject();
        bodyJson.put("LoanDblNo",loanDblNo);
        bodyJson.put("IdentCrdNo",identCrdNo);
        objJson.put("Body",bodyJson);
        String requestBody=objJson.toJSONString();

        log.info("SendSms:{}",requestBody);

        try {
            String sendUrl=smsUrl+"/NDCS";
            // 发送 POST 请求
            HttpResponse<JsonNode> jsonResponse = Unirest.post(sendUrl)
                    .header("Content-Type", "application/json;charset=UTF-8")
                    .body(requestBody)
                    .asJson();

            int status = jsonResponse.getStatus();
            if (status == 200) {
                log.info("SendSms.Response:{}", jsonResponse.getBody());

                String subRetCd = JSONObject.parseObject(jsonResponse.getBody().getObject()
                        .get("Head").toString()).get("SubRetCd").toString();

                log.info("subRetCd::{}",subRetCd);
                if(subRetCd.equals("F")){
                    throw new ServiceException(EnumRCode.NoData.value, EnumRCode.NoData.name);
                }

                JSONObject jsonObject = JSONObject.parseObject(jsonResponse.getBody().getObject().get("Body").toString());

                JSONArray repymtPlanArry = jsonObject.getJSONArray("RepymtPlanArry");
                for (int i = 0; i < repymtPlanArry.size(); i++) {
                    JSONObject object = repymtPlanArry.getJSONObject(i);

//                    BigDecimal rpyblPnpAmt=new BigDecimal(object.get("RpyblPnpAmt").toString());
//                    BigDecimal RpyblInt=new BigDecimal(object.get("RpyblInt").toString());
//                    BigDecimal RpyblPnyInt=new BigDecimal(object.get("RpyblPnyInt").toString());
//                    BigDecimal RpyblFee=new BigDecimal(object.get("RpyblFee").toString());

                    //应还金额
                    BigDecimal repaidAmount=new BigDecimal(object.get("RpyblPnpAmt").toString())
                            .add(new BigDecimal(object.get("RpyblInt").toString()))
                            .add(new BigDecimal(object.get("RpyblPnyInt").toString()))
                            .add(new BigDecimal(object.get("RpyblFee").toString()));//应还金额

                    BigDecimal remainpay=repaidAmount
                            .subtract(new BigDecimal(object.get("ActRepyPnpAmt").toString()))
                            .subtract(new BigDecimal(object.get("ActRepyInt").toString()))
                            .subtract(new BigDecimal(object.get("ActRepyPnyInt").toString()))
                            .subtract(new BigDecimal(object.get("ActRepyFee").toString()));//实还金额

                    object.put("remainpay",remainpay);//剩余金额
                    object.put("LoanDblNo",jsonObject.get("LoanDblNo"));//借据编号
                    object.put("TotLineNum",jsonObject.get("TotLineNum"));//总期数
                }

//                jsonObject.remove("LoanDblNo");//移除借据编号
//                jsonObject.remove("TotLineNum");//移除总期数
                jsonObject.remove("IdEntCrdNo");//移除身份证号

//                {
//                    "LoanDblNo":"借据编号",
//                    "TotLineNum":"总期数",
//                    "remainpay":"剩余金额"
//                }

                return LineAPIR.Success("ok",jsonObject);
            } else {
                log.warn("SendSms.Fail:Status={},Cause={}", status, jsonResponse.getStatusText());
                return LineAPIR.Error(status,jsonResponse.getStatusText());
            }
        }
        catch (Exception e){
            log.warn("SendSms.Err:{}", e.getMessage());
            return LineAPIR.Error(-1,e.getMessage());
        }
    }



    /**
     * 智慧线上还款记录
     * @return
     */
    public LineAPIR getSOFPRepaySerial(String loanDblNo,String identCrdNo){

//        String adrIP= JPubUtils.getLocalIpAddr();

        JSONObject objJson=new JSONObject();
        Date currentDate =new Date();
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmmssSSS");
        SimpleDateFormat TxnDtF = new SimpleDateFormat("yyyyMMdd");
//        SimpleDateFormat RqsTmF = new SimpleDateFormat("yyyy-MM-dd HH:MM:SS");
        Random random=new Random();
        String tDt=sdf.format(currentDate)+(100+random.nextInt(900));
        String TxnDt= TxnDtF.format(currentDate);
//        String RqsTm = RqsTmF.format(currentDate);

        JSONObject headJson=new JSONObject();
        headJson.put("SvcCd","30230005");
        headJson.put("SvcScn","05");
        headJson.put("UsrLng","CHINESE");
        headJson.put("MdulId","RB");
        headJson.put("CnlTp","07");
        headJson.put("CnsmrSysId","101500");
        headJson.put("CnsmrSeqNo",tDt);
        headJson.put("BsnSeqNo",tDt);
        headJson.put("SrcCnsmrSysId","100900");
        headJson.put("SrcCnsmrSeqNo",tDt);
        headJson.put("TxnDt",TxnDt);//时间戳
        headJson.put("TxnTm","195242800");
        headJson.put("CnsmrSvrId","1234567");
        headJson.put("FileFlg","0");
//        headJson.put("InstId","50801");
//        headJson.put("TlrNo","V0016");

        objJson.put("Head",headJson);
        JSONObject bodyJson=new JSONObject();
        bodyJson.put("LoanDblNo",loanDblNo);
        bodyJson.put("IdentCrdNo",identCrdNo);
        objJson.put("Body",bodyJson);
        String requestBody=objJson.toJSONString();

        log.info("SendSms:{}",requestBody);

        try {
            String sendUrl=smsUrl+"/NDCS";
            // 发送 POST 请求
            HttpResponse<JsonNode> jsonResponse = Unirest.post(sendUrl)
                    .header("Content-Type", "application/json;charset=UTF-8")
                    .body(requestBody)
                    .asJson();

            int status = jsonResponse.getStatus();
            if (status == 200) {
                log.info("SendSms.Response:{}", jsonResponse.getBody());

                String subRetCd = JSONObject.parseObject(jsonResponse.getBody().getObject()
                        .get("Head").toString()).get("SubRetCd").toString();

                log.info("subRetCd::{}",subRetCd);
                if(subRetCd.equals("F")){
                    throw new ServiceException(EnumRCode.NoData.value, EnumRCode.NoData.name);
                }



                JSONObject jsonObject = JSONObject.parseObject(jsonResponse.getBody().getObject().get("Body").toString());

                JSONArray repymtPlanArry = jsonObject.getJSONArray("RepymtSeqArry");

                for (int i = 0; i < repymtPlanArry.size(); i++) {
                    JSONObject object = repymtPlanArry.getJSONObject(i);

                    BigDecimal repayment=new BigDecimal(object.get("RpyblPnpAmt").toString())
                            .add(new BigDecimal(object.get("RpyblInt").toString()))
                            .add(new BigDecimal(object.get("RpyblPnyInt").toString()))
                            .add(new BigDecimal(object.get("RpyblFee").toString()));
                    object.put("repayment",repayment);//实际还款总金额
                    object.put("LoanDblNo",jsonObject.get("LoanDblNo"));//借据号
                }

                jsonObject.remove("IdentCrdNo");//移除身份证号


                return LineAPIR.Success("ok",jsonObject);
            } else {
                log.warn("SendSms.Fail:Status={},Cause={}", status, jsonResponse.getStatusText());
                return LineAPIR.Error(status,jsonResponse.getStatusText());
            }
        }
        catch (Exception e){
            log.warn("SendSms.Err:{}", e.getMessage());
            return LineAPIR.Error(-1,e.getMessage());
        }
    }




    /**
     * 智慧线上还款试算
     * @return
     */
    public LineAPIR getSOFPTrial(String loanReqNo,String trialTp){

//        String adrIP= JPubUtils.getLocalIpAddr();

        JSONObject objJson=new JSONObject();
        Date currentDate =new Date();
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmmssSSS");
        SimpleDateFormat TxnDtF = new SimpleDateFormat("yyyyMMdd");
//        SimpleDateFormat RqsTmF = new SimpleDateFormat("yyyy-MM-dd HH:MM:SS");
        Random random=new Random();
        String tDt=sdf.format(currentDate)+(100+random.nextInt(900));
        String TxnDt= TxnDtF.format(currentDate);
//        String RqsTm = RqsTmF.format(currentDate);

        JSONObject headJson=new JSONObject();
        headJson.put("SvcCd","30230001");
        headJson.put("SvcScn","52");
        headJson.put("UsrLng","CHINESE");
        headJson.put("MdulId","RB");
        headJson.put("CnlTp","07");
        headJson.put("CnsmrSysId","101500");
        headJson.put("CnsmrSeqNo",tDt);
        headJson.put("BsnSeqNo",tDt);
        headJson.put("SrcCnsmrSysId","100900");
        headJson.put("SrcCnsmrSeqNo",tDt);
        headJson.put("TxnDt",TxnDt);//时间戳
        headJson.put("TxnTm","195242800");
        headJson.put("CnsmrSvrId","1234567");
        headJson.put("FileFlg","0");
//        headJson.put("InstId","50801");
//        headJson.put("TlrNo","V0016");

        objJson.put("Head",headJson);
        JSONObject bodyJson=new JSONObject();
        bodyJson.put("LoanReqNo",loanReqNo);
        bodyJson.put("BsnTp","repayCalc");//固定值
        bodyJson.put("TrialTp",trialTp);
        objJson.put("Body",bodyJson);
        String requestBody=objJson.toJSONString();

        log.info("SendSms:{}",requestBody);

        try {
            String sendUrl=smsUrl+"/NDCS";
            // 发送 POST 请求
            HttpResponse<JsonNode> jsonResponse = Unirest.post(sendUrl)
                    .header("Content-Type", "application/json;charset=UTF-8")
                    .body(requestBody)
                    .asJson();

            int status = jsonResponse.getStatus();
            if (status == 200) {
                log.info("SendSms.Response:{}", jsonResponse.getBody());

                String subRetCd = JSONObject.parseObject(jsonResponse.getBody().getObject()
                        .get("Head").toString()).get("SubRetCd").toString();

                log.info("subRetCd::{}",subRetCd);
                if(subRetCd.equals("F")){
                    throw new ServiceException(EnumRCode.NoData.value, EnumRCode.NoData.name);
                }

                JSONObject jsonObject = JSONObject.parseObject(jsonResponse.getBody().getObject().get("Body").toString());

                JSONObject json=new JSONObject();
                JSONArray jsonArray=new JSONArray();
                jsonArray.add(jsonObject);
                json.put("calcArray",jsonArray);

                return LineAPIR.Success("ok",json);
            } else {
                log.warn("SendSms.Fail:Status={},Cause={}", status, jsonResponse.getStatusText());
                return LineAPIR.Error(status,jsonResponse.getStatusText());
            }
        }
        catch (Exception e){
            log.warn("SendSms.Err:{}", e.getMessage());
            return LineAPIR.Error(-1,e.getMessage());
        }
    }



    /**
     * 智慧线上还款结果
     * @return
     */
    public LineAPIR getSOFPRepaymentResult(String aplyNo1,String loanDblNo,String idEntCrdNo){

//        String adrIP= JPubUtils.getLocalIpAddr();

        JSONObject objJson=new JSONObject();
        Date currentDate =new Date();
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmmssSSS");
        SimpleDateFormat TxnDtF = new SimpleDateFormat("yyyyMMdd");
//        SimpleDateFormat RqsTmF = new SimpleDateFormat("yyyy-MM-dd HH:MM:SS");
        Random random=new Random();
        String tDt=sdf.format(currentDate)+(100+random.nextInt(900));
        String TxnDt= TxnDtF.format(currentDate);
//        String RqsTm = RqsTmF.format(currentDate);

        JSONObject headJson=new JSONObject();
        headJson.put("SvcCd","30230005");
        headJson.put("SvcScn","06");
        headJson.put("UsrLng","CHINESE");
        headJson.put("MdulId","RB");
        headJson.put("CnlTp","07");
        headJson.put("CnsmrSysId","101500");
        headJson.put("CnsmrSeqNo",tDt);
        headJson.put("BsnSeqNo",tDt);
        headJson.put("SrcCnsmrSysId","100900");
        headJson.put("SrcCnsmrSeqNo",tDt);
        headJson.put("TxnDt",TxnDt);//时间戳
        headJson.put("TxnTm","195242800");
        headJson.put("CnsmrSvrId","1234567");
        headJson.put("FileFlg","0");
//        headJson.put("InstId","50801");
//        headJson.put("TlrNo","V0016");

        objJson.put("Head",headJson);
        JSONObject bodyJson=new JSONObject();
        bodyJson.put("AplyNo1","NDCS"+aplyNo1);
        bodyJson.put("LoanDblNo",loanDblNo);
        bodyJson.put("IdentCrdNo",idEntCrdNo);
        objJson.put("Body",bodyJson);
        String requestBody=objJson.toJSONString();

        log.info("SendSms:{}",requestBody);

        try {
            String sendUrl=smsUrl+"/NDCS";
            // 发送 POST 请求
            HttpResponse<JsonNode> jsonResponse = Unirest.post(sendUrl)
                    .header("Content-Type", "application/json;charset=UTF-8")
                    .body(requestBody)
                    .asJson();

            int status = jsonResponse.getStatus();
            if (status == 200) {
                log.info("SendSms.Response:{}", jsonResponse.getBody());

                String subRetCd = JSONObject.parseObject(jsonResponse.getBody().getObject()
                        .get("Head").toString()).get("SubRetCd").toString();

                log.info("subRetCd::{}",subRetCd);
                if(subRetCd.equals("F")){
                    throw new ServiceException(EnumRCode.NoData.value, EnumRCode.NoData.name);
                }

                return LineAPIR.Success("ok",JSONObject.parseObject(jsonResponse.getBody().getObject().get("Body").toString()));
            } else {
                log.warn("SendSms.Fail:Status={},Cause={}", status, jsonResponse.getStatusText());
                return LineAPIR.Error(status,jsonResponse.getStatusText());
            }
        }
        catch (Exception e){
            log.warn("SendSms.Err:{}", e.getMessage());
            return LineAPIR.Error(-1,e.getMessage());
        }
    }


    /**
     * 智慧线上还款申请_一键扣款
     * @param aplyNo1 申请号
     * @param loanDblNo 借据号
     * @param idEntCrdNo 身份证号
     * @param acctNo 账户
     * @param repymtAmt1 还款金额
     * @param repymtTrmNum1 还款期数
     * @param repymtTm 还款时间
     * @return
     */
    public LineAPIR SOFPRepaymentApplyDeduction(String aplyNo1,
                                                String loanDblNo,
                                                String idEntCrdNo,
                                                String acctNo,
                                                String repymtAmt1,
                                                String repymtTrmNum1,
                                                String repymtTm){

//        String adrIP= JPubUtils.getLocalIpAddr();

        JSONObject objJson=new JSONObject();
        Date currentDate =new Date();
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmmssSSS");
        SimpleDateFormat TxnDtF = new SimpleDateFormat("yyyyMMdd");
//        SimpleDateFormat RqsTmF = new SimpleDateFormat("yyyy-MM-dd HH:MM:SS");
        Random random=new Random();
        String tDt=sdf.format(currentDate)+(100+random.nextInt(900));
        String TxnDt= TxnDtF.format(currentDate);
//        String RqsTm = RqsTmF.format(currentDate);

        JSONObject headJson=new JSONObject();
        headJson.put("SvcCd","30220011");
        headJson.put("SvcScn","01");
        headJson.put("UsrLng","CHINESE");
        headJson.put("MdulId","RB");
        headJson.put("CnlTp","07");
        headJson.put("CnsmrSysId","101500");
        headJson.put("CnsmrSeqNo",tDt);
        headJson.put("BsnSeqNo",tDt);
        headJson.put("SrcCnsmrSysId","100900");
        headJson.put("SrcCnsmrSeqNo",tDt);
        headJson.put("TxnDt",TxnDt);//时间戳
        headJson.put("TxnTm","195242800");
        headJson.put("CnsmrSvrId","1234567");
        headJson.put("FileFlg","0");
//        headJson.put("InstId","50801");
//        headJson.put("TlrNo","V0016");

        objJson.put("Head",headJson);
        JSONObject bodyJson=new JSONObject();
        bodyJson.put("AplyNo1","NDCS"+aplyNo1);
        bodyJson.put("LoanDblNo",loanDblNo);
        bodyJson.put("IdentCrdNo",idEntCrdNo);
        bodyJson.put("AcctNo",acctNo);
        bodyJson.put("RepymtTp1","2");
        bodyJson.put("RepymtAmt1",repymtAmt1);
        bodyJson.put("RepymtTrmNum1",repymtTrmNum1);
        bodyJson.put("RepymtTm",repymtTm);
        objJson.put("Body",bodyJson);
        String requestBody=objJson.toJSONString();

        log.info("SendSms:{}",requestBody);

        try {
            String sendUrl=smsUrl+"/NDCS";
            // 发送 POST 请求
            HttpResponse<JsonNode> jsonResponse = Unirest.post(sendUrl)
                    .header("Content-Type", "application/json;charset=UTF-8")
                    .body(requestBody)
                    .asJson();

            int status = jsonResponse.getStatus();
            if (status == 200) {
                log.info("SendSms.Response:{}", jsonResponse.getBody());

                String subRetCd = JSONObject.parseObject(jsonResponse.getBody().getObject()
                        .get("Head").toString()).get("SubRetCd").toString();

                log.info("subRetCd::{}",subRetCd);
                if(subRetCd.equals("F")){

                    String retMsg = JSONObject.parseObject(jsonResponse.getBody().getObject()
                            .get("Head").toString()).get("RetMsg").toString();
                    throw new ServiceException(EnumRCode.NoData.value, retMsg);
                }

                JSONObject jsonObject = JSONObject.parseObject(jsonResponse.getBody().getObject().get("Body").toString());

                return LineAPIR.Success("ok",jsonObject);
            } else {
                log.warn("SendSms.Fail:Status={},Cause={}", status, jsonResponse.getStatusText());
                return LineAPIR.Error(status,jsonResponse.getStatusText());
            }
        }
        catch (Exception e){
            log.warn("SendSms.Err:{}", e.getMessage());
            return LineAPIR.Error(-1,e.getMessage());
        }
    }

    /**
     * 智慧线上还款申请_支付宝
     * @param aplyNo1 申请号
     * @param loanDblNo 借据号
     * @param pdNo1 产品编号
     * @param repymtAmt1 还款金额
     * @param repymtTrmNum1 还款期数
     * @param repymtTm 还款时间
     * @return
     *  ShrtMsgSrlNo 短信流水号
     *  RepymtSt 还款状态
     */
    public LineAPIR SOFPRepaymentApplyZFB(String aplyNo1,
                                          String loanDblNo,
                                          String pdNo1,
                                          String repymtAmt1,
                                          String repymtTrmNum1,
                                          String repymtTm){

//        String adrIP= JPubUtils.getLocalIpAddr();

        JSONObject objJson=new JSONObject();
        Date currentDate =new Date();
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmmssSSS");
        SimpleDateFormat TxnDtF = new SimpleDateFormat("yyyyMMdd");
//        SimpleDateFormat RqsTmF = new SimpleDateFormat("yyyy-MM-dd HH:MM:SS");
        Random random=new Random();
        String tDt=sdf.format(currentDate)+(100+random.nextInt(900));
        String TxnDt= TxnDtF.format(currentDate);
//        String RqsTm = RqsTmF.format(currentDate);

        JSONObject headJson=new JSONObject();
        headJson.put("SvcCd","30210001");
        headJson.put("SvcScn","06");
        headJson.put("UsrLng","CHINESE");
        headJson.put("MdulId","RB");
        headJson.put("CnlTp","07");
        headJson.put("CnsmrSysId","101500");
        headJson.put("CnsmrSeqNo",tDt);
        headJson.put("BsnSeqNo",tDt);
        headJson.put("SrcCnsmrSysId","100900");
        headJson.put("SrcCnsmrSeqNo",tDt);
        headJson.put("TxnDt",TxnDt);//时间戳
        headJson.put("TxnTm","195242800");
        headJson.put("CnsmrSvrId","1234567");
        headJson.put("FileFlg","0");
//        headJson.put("InstId","50801");
//        headJson.put("TlrNo","V0016");

        objJson.put("Head",headJson);
        JSONObject bodyJson=new JSONObject();
        bodyJson.put("AplyNo1","NDCS"+aplyNo1);
        bodyJson.put("LoanDblNo",loanDblNo);
        bodyJson.put("PdNo1",pdNo1);
        bodyJson.put("RepymtTp1","1");
        bodyJson.put("RepymtAmt1",repymtAmt1);
        bodyJson.put("RepymtTrmNum1",repymtTrmNum1);
        bodyJson.put("RepymtTm",repymtTm);
        objJson.put("Body",bodyJson);
        String requestBody=objJson.toJSONString();

        log.info("SendSms:{}",requestBody);

        try {
            String sendUrl=smsUrl+"/NDCS";
            // 发送 POST 请求
            HttpResponse<JsonNode> jsonResponse = Unirest.post(sendUrl)
                    .header("Content-Type", "application/json;charset=UTF-8")
                    .body(requestBody)
                    .asJson();

            int status = jsonResponse.getStatus();
            if (status == 200) {
                log.info("SendSms.Response:{}", jsonResponse.getBody());

                String subRetCd = JSONObject.parseObject(jsonResponse.getBody().getObject()
                        .get("Head").toString()).get("SubRetCd").toString();

                log.info("subRetCd::{}",subRetCd);
                if(subRetCd.equals("F")){

                    String retMsg = JSONObject.parseObject(jsonResponse.getBody().getObject()
                            .get("Head").toString()).get("RetMsg").toString();
                    throw new ServiceException(EnumRCode.NoData.value, retMsg);
                }

                return LineAPIR.Success("ok",JSONObject.parseObject(jsonResponse.getBody().getObject().get("Body").toString()));
            } else {
                log.warn("SendSms.Fail:Status={},Cause={}", status, jsonResponse.getStatusText());
                return LineAPIR.Error(status,jsonResponse.getStatusText());
            }
        }
        catch (Exception e){
            log.warn("SendSms.Err:{}", e.getMessage());
            return LineAPIR.Error(-1,e.getMessage());
        }
    }

    /**
     * C卡进件申请服务 (交易码 CAFP_ESB_APPLY_01)
     * @param applyId 请求流水号  以系统简称开头，每个申请唯一，例如：
     * 1）系统简称（4位数，不足四位X补齐）+时间串（毫秒，17位）+序号（8位） 或者
     * 2) RCR(系统编号)+07(渠道号)+ANTI（业务编码）+20位雪花算法
     * @param productCode  产品编号
     * @param workflowcode  决策流编号	反欺诈平台提供
     * @param ecifcustid  客户流水号 ecif客户号
     * @param customerName  姓名
     * @param certType  证件类型
     * @param certId 证件号码
     * @param dueDays 逾期天数 (非必传)
     * @param duePeriod 逾期阶段 (非必传)
     * @param riskData 风控扩展字段
     * @return
     */
    public LineAPIR CAFP0001(String applyId,
                                          String productCode,
                                          String workflowcode,
                                          String ecifcustid,
                                          String customerName,
                                          String certType,
                                          String certId,
                                          String dueDays,
                                          String duePeriod,
                                          String riskData){

//        String adrIP= JPubUtils.getLocalIpAddr();

        JSONObject objJson=new JSONObject();
        Date currentDate =new Date();
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmmssSSS");
        SimpleDateFormat TxnDtF = new SimpleDateFormat("yyyyMMdd");
//        SimpleDateFormat RqsTmF = new SimpleDateFormat("yyyy-MM-dd HH:MM:SS");
        Random random=new Random();
        String tDt=sdf.format(currentDate)+(100+random.nextInt(900));
        String TxnDt= TxnDtF.format(currentDate);
//        String RqsTm = RqsTmF.format(currentDate);

        //40220004
        JSONObject headJson=new JSONObject();
        headJson.put("SvcCd", "40220004");  //服务码
        headJson.put("SvcScn", "07");  //场景码
        headJson.put("TxnCd", "CAFP_ESB_APPLY_01");  //交易码
        headJson.put("UsrLng","CHINESE");
        headJson.put("MdulId","RB");
        headJson.put("CnlTp","07");
        headJson.put("CnsmrSysId","101500");
        headJson.put("CnsmrSeqNo",tDt);
        headJson.put("BsnSeqNo",tDt);
        headJson.put("SrcCnsmrSysId","100900");
        headJson.put("SrcCnsmrSeqNo",tDt);
        headJson.put("TxnDt",TxnDt);//时间戳
        headJson.put("TxnTm","195242800");
        headJson.put("CnsmrSvrId","1234567");
        headJson.put("FileFlg","0");
//        headJson.put("InstId","50801");
//        headJson.put("TlrNo","V0016");

        objJson.put("Head",headJson);
        JSONObject bodyJson=new JSONObject();
        JSONObject infCntnt=new JSONObject();
        infCntnt.put("applyId",applyId);
        infCntnt.put("productCode",productCode);
        infCntnt.put("workflowcode",workflowcode);
        infCntnt.put("ecifcustid",ecifcustid);
        infCntnt.put("customerName",customerName);
        infCntnt.put("certType",certType);
        infCntnt.put("certId",certId);
        infCntnt.put("dueDays",dueDays);
        infCntnt.put("duePeriod",duePeriod);
        infCntnt.put("riskData",riskData);

        bodyJson.put("SvcNm","NEW_COLLECTION_APPLY");//RCDJYD_PRECREDIT_APPLY
        bodyJson.put("InfCntnt",infCntnt.toString());
        objJson.put("Body",bodyJson);
        String requestBody=objJson.toJSONString();

        log.debug("SendSms:{}",requestBody);

        try {
            String sendUrl=smsUrl+"/NDCS";
            // 发送 POST 请求
            HttpResponse<JsonNode> jsonResponse = Unirest.post(sendUrl)
                    .header("Content-Type", "application/json;charset=UTF-8")
                    .body(requestBody)
                    .asJson();

            int status = jsonResponse.getStatus();
            if (status == 200) {
                log.debug("SendSms.Response:{}", jsonResponse.getBody());
                /**
                 * "Body": {
                 * 		"ScssFlg": "true",
                 * 		"RetrnCd": "调用成功",
                 * 		"RetrnMsg": "{\"reasonCode\":9000,\"reasonDesc\":\"调用成功\",\"reasonDetail\":\"异步执行\",\"applyId\":\"NDCS2025060317035040000000001\",\"workflowCode\":\"CCARD_COLLECTION_DECISION_NO\",\"productCode\":\"NEWC11000011\",\"token\":\"tokenb417c719db1742658a0d23e6836a33da-1748880000000\"}"
                 * 	    }
                 */
//                String subRetCd = JSONObject.parseObject(jsonResponse.getBody().getObject()
//                        .get("Head").toString()).get("SubRetCd").toString();

//                log.info("subRetCd::{}",subRetCd);
//                if(subRetCd.equals("F")){
//
//                    String retMsg = JSONObject.parseObject(jsonResponse.getBody().getObject()
//                            .get("Head").toString()).get("RetMsg").toString();
//                    throw new ServiceException(EnumRCode.NoData.value, retMsg);
//                }

                return LineAPIR.Success("ok",JSONObject.parseObject(jsonResponse.getBody().getObject().get("Body").toString()));
            } else {
                log.warn("SendSms.Fail:Status={},Cause={}", status, jsonResponse.getStatusText());
                return LineAPIR.Error(status,jsonResponse.getStatusText());
            }
        }
        catch (Exception e){
            log.warn("SendSms.Err:{}", e.getMessage());
            return LineAPIR.Error(-1,e.getMessage());
        }
    }
    /**
     * C卡进件查询服务 (交易码 CAFP_ESB_QUERY_01)
     * @param applyId 请求流水号  以系统简称开头，每个申请唯一，例如：
     * 1）系统简称（4位数，不足四位X补齐）+时间串（毫秒，17位）+序号（8位） 或者
     * 2) RCR(系统编号)+07(渠道号)+ANTI（业务编码）+20位雪花算法
     * @return
     */
    public LineAPIR CAFP0002(String applyId){

//        String adrIP= JPubUtils.getLocalIpAddr();

        JSONObject objJson=new JSONObject();
        Date currentDate =new Date();
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmmssSSS");
        SimpleDateFormat TxnDtF = new SimpleDateFormat("yyyyMMdd");
//        SimpleDateFormat RqsTmF = new SimpleDateFormat("yyyy-MM-dd HH:MM:SS");
        Random random=new Random();
        String tDt=sdf.format(currentDate)+(100+random.nextInt(900));
        String TxnDt= TxnDtF.format(currentDate);
//        String RqsTm = RqsTmF.format(currentDate);

        //40220004
        JSONObject headJson=new JSONObject();
        headJson.put("SvcCd", "40220004");  //服务码
        headJson.put("SvcScn", "08");  //场景码
        headJson.put("TxnCd", "CAFP_ESB_QUERY_01");  //交易码
        headJson.put("UsrLng","CHINESE");
        headJson.put("MdulId","RB");
        headJson.put("CnlTp","07");
        headJson.put("CnsmrSysId","101500");
        headJson.put("CnsmrSeqNo",tDt);
        headJson.put("BsnSeqNo",tDt);
        headJson.put("SrcCnsmrSysId","100900");
        headJson.put("SrcCnsmrSeqNo",tDt);
        headJson.put("TxnDt",TxnDt);//时间戳
        headJson.put("TxnTm","195242800");
        headJson.put("CnsmrSvrId","1234567");
        headJson.put("FileFlg","0");
//        headJson.put("InstId","50801");
//        headJson.put("TlrNo","V0016");

        objJson.put("Head",headJson);
        JSONObject bodyJson=new JSONObject();
        JSONObject infCntnt=new JSONObject();
        infCntnt.put("applyId",applyId);


        bodyJson.put("SvcNm","NEW_COLLECTION_QUERY");//RCDJYD_PRECREDIT_APPLY
        bodyJson.put("InfCntnt",infCntnt.toString());
        objJson.put("Body",bodyJson);
        String requestBody=objJson.toJSONString();

        log.debug("CAFP0002:{}",requestBody);

        try {
            String sendUrl=smsUrl+"/NDCS";
            // 发送 POST 请求
            HttpResponse<JsonNode> jsonResponse = Unirest.post(sendUrl)
                    .header("Content-Type", "application/json;charset=UTF-8")
                    .body(requestBody)
                    .asJson();

            int status = jsonResponse.getStatus();
            if (status == 200) {
                log.debug("CAFP0002.Response:{}", jsonResponse.getBody());
                /**
                 * "Body": {
                 * 		"ScssFlg": "true",
                 * 		"RetrnCd": "调用成功",
                 * 		"RetrnMsg": "{\"reasonCode\":9000,\"reasonDesc\":\"调用成功\",\"reasonDetail\":\"异步执行\",\"applyId\":\"NDCS2025060317035040000000001\",\"workflowCode\":\"CCARD_COLLECTION_DECISION_NO\",\"productCode\":\"NEWC11000011\",\"token\":\"tokenb417c719db1742658a0d23e6836a33da-1748880000000\"}"
                 * 	    }
                 */
//                String subRetCd = JSONObject.parseObject(jsonResponse.getBody().getObject()
//                        .get("Head").toString()).get("SubRetCd").toString();

//                log.info("subRetCd::{}",subRetCd);
//                if(subRetCd.equals("F")){
//
//                    String retMsg = JSONObject.parseObject(jsonResponse.getBody().getObject()
//                            .get("Head").toString()).get("RetMsg").toString();
//                    throw new ServiceException(EnumRCode.NoData.value, retMsg);
//                }

                return LineAPIR.Success("ok",JSONObject.parseObject(jsonResponse.getBody().getObject().get("Body").toString()));
            } else {
                log.warn("CAFP0002.Fail:Status={},Cause={}", status, jsonResponse.getStatusText());
                return LineAPIR.Error(status,jsonResponse.getStatusText());
            }
        }
        catch (Exception e){
            log.warn("CAFP0002.Err:{}", e.getMessage());
            return LineAPIR.Error(-1,e.getMessage());
        }
    }



}
